package tgc.edu.exam.controller;

import java.util.HashSet;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.exam.custom.DataGridParam;
import tgc.edu.exam.custom.DataGridUtils;
import tgc.edu.exam.entity.BookRecord;
import tgc.edu.exam.entity.Books;
import tgc.edu.exam.form.RecordForm;
import tgc.edu.exam.service.BookRecordService;
import tgc.edu.exam.service.BookService;

@Controller
@RequestMapping("/audit")
public class AuditController {

	@Autowired
	private BookRecordService recordService;
	@Autowired
	private BookService bookService;
	
	@RequestMapping("/manage")
	public String manage() {
		return "audit/manage";
	}
	
	@RequestMapping("/page")
	@ResponseBody
	public Object page(DataGridParam param,RecordForm form) {
		Sort sort = new Sort(Direction.DESC, "id");
		Pageable pageable = param.getPageable(sort);
		Specification<BookRecord> spec =  buildSpec(form);
		Page<BookRecord> page = recordService.findAll(spec,pageable);
		return DataGridUtils.buildResult(page);
	}

	private Specification<BookRecord> buildSpec(RecordForm form) {
		Specification<BookRecord> spec = new Specification<BookRecord>() {
			private static final long serialVersionUID = 1L;
			@Override
			public Predicate toPredicate(Root<BookRecord> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				String state = "等待管理员审核";
				if (StringUtils.hasText(state)) {
					rules.add(cb.like(root.get("state"), "%"+state+"%"));
				}
				if(StringUtils.hasText(form.getUsername())) {
					rules.add(cb.like(root.get("username"), "%"+form.getUsername()+"%"));
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}
	
	@RequestMapping("/pass")
	@ResponseBody
	public void pass(Integer id,Integer p) {
		BookRecord model = recordService.findById(id);
		if(p==1) {
			model.setState("已借");
			recordService.save(model);
		}else {
			BookRecord record = recordService.findById(id);
			Books book = bookService.findByByid(record.getByid());
			System.out.println(book.getTitle()+"-------");
			Integer number = book.getNumber()+1;
			book.setNumber(number);
			bookService.save(book);
			model.setState("已拒绝");
			recordService.save(model);
		}
	}
	
	
	
	@RequestMapping("/boandren")
	public String boandren() {
		return "audit/boandren";
	}
	
	@RequestMapping("/boandrenpage")
	@ResponseBody
	public Object boandrenpage(DataGridParam param,RecordForm form) {
		Sort sort = new Sort(Direction.DESC, "id");
		Pageable pageable = param.getPageable(sort);
		Specification<BookRecord> spec =  buildSpecboandren(form);
		Page<BookRecord> page = recordService.findAll(spec,pageable);
		return DataGridUtils.buildResult(page);
	}

	private Specification<BookRecord> buildSpecboandren(RecordForm form) {
		Specification<BookRecord> spec = new Specification<BookRecord>() {
			private static final long serialVersionUID = 1L;
			@Override
			public Predicate toPredicate(Root<BookRecord> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				String state = "已借";
				if (StringUtils.hasText(state)) {
					Predicate name = cb.like(root.get("state"), "%已借%");
					Predicate code = cb.like(root.get("state"), "%已还%");
					Predicate p = cb.like(root.get("state"), "%已拒绝%");
					Predicate or = cb.or(name, code,p);
					rules.add(or);
					
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}
}
